2
2
.
.
7
7
.
.
2
2
@
@
O
O
n
n
e
e
T
T
o
o
O
O
n
n
e
e
-
-
F
F
o
o
r
r
e
e
i
i
g
g
n
n
K
K
e
e
y
y
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
@OneToOne relationship is used when you want to split data into multiple Tables.
For instance instead of having single Table with 20 columns you can have 2 Tables with 10 columns each.
One Table will have an additional column that points to a related Record in the second Table.
@OneToOne relationship can be useful when you want to logically group data
first Table can contain Customer's basic information like: FirstName, LastName, Age
second Table can contain Customer's contact information like: Email, Phone, Address
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller and @RequestMapping. Includes Tomcat Server.
SQL
Spring Data JPA
Enables @Entity and @Id
SQL
H2 Database
Enables in-memory H2 Database
DB Schema
Syntax
@OneToOne(cascade = CascadeType.ALL)
public AddressEntity addressEntity;
AUTHOR_ENTITY
ADDRESS_ENTITY_ID
NAME
AGE
ADDRESS_ENTITY
CITY
STREET
ID
ID
MyController
http://localhost:8080/AddAuthor
Author
AuthorRepository
addAuthor()
Address
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: relationships_onetoone (add Spring Boot Starters from the table)
Edit FIle: application.properties (specify H2 DB name & enable H2 Web Console)
Create Package: entities (inside main package)
Create Class: Author.java (inside package entities)
Create Class: Address.java (inside package entities)
Create Package: repositories (inside main package)
Create Interface: AuthorRepository.java (inside package repositories)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside package controllers)
application.properties
spring.datasource.url = jdbc:h2:mem:testdb
spring.h2.console.enabled = true
Author.java
package com.ivoronline.relationships_onetoone.entities;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.OneToOne;
import javax.persistence.CascadeType;
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
public String name;
public Integer age;
@OneToOne(cascade = CascadeType.ALL)
public Address address;
}
Address.java
package com.ivoronline.relationships_onetoone.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
public String city;
public String street;
}
AuthorRepository.java
package com.ivoronline.relationships_onetoone.repositories;
import com.ivoronline.relationships_onetoone.entities.Author;
import org.springframework.data.repository.CrudRepository;
public interface AuthorRepository extends CrudRepository<Author, Integer> { }
MyController.java
package com.ivoronline.relationships_onetoone.controllers;
import com.ivoronline.relationships_onetoone.entities.Author;
import com.ivoronline.relationships_onetoone.entities.Address;
import com.ivoronline.relationships_onetoone.repositories.AuthorRepository;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@Autowired
AuthorRepository authorRepository;
@ResponseBody
@RequestMapping("/AddAuthor")
public String addAuthor() {
//CREATE ADDRESS ENTITY
Address address = new Address();
address.city = "London";
address.street = "Piccadilly";
//CREATE AUTHOR ENTITY
Author author = new Author();
author.name = "John";
author.age = 20;
author.address = address;
//STORE AUTHOR/ADDRESS ENTITY INTO DB
authorRepository.save(author);
//RETURN SOMETHING TO BROWSER
return "Author/Address was stored into DB";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/AddAuthor
http://localhost:8080/h2-console (Open H2 Console)
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>